<template>
	<view class="page-box">
		<view class="order" v-for="(item, index) in list" :key="item.goodsId">
			<view class="top">
				<view class="left">
					<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
					<view class="store">{{ item.goodsName }}</view>
					<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
				</view>
			</view>
			<view class="item">
				<view class="left">
					<image :src="item.image.split(',')[0]" mode="aspectFill"></image>
				</view>
				<view class="content">
					<view class="title u-line-2">{{ item.goodsDesc }}</view>
					<view class="type">{{ item.address }}</view>
					<view class="delivery-time">发货时间 {{ item.createTime }}</view>
				</view>
				<view class="right">
					<view class="price" style="color: red;">
						￥{{ item.goodsPrice }}元
					</view>
					<view class="number">x1</view>
				</view>
			</view>
			<view class="bottom">
				<view class="logistics btn" @click="lookBtn(item)">查看</view>
				<view @click="upanddownBtn(item)" class="exchange btn">
					<spna v-if="item.status == '0'">下架</spna>
					<spna v-if="item.status == '1'">上架</spna>
				</view>
				<view class="evaluate btn" @click="editBtn(item)">编辑</view>
				<view class="evaluate btn" @click="deleteBtn(item)">删除</view>
			</view>
		</view>
		<u-loadmore bgColor="#f2f2f2" :status="loadstatus"></u-loadmore>

		<!-- 删除弹窗 -->
		<u-popup :mask-close-able="false" border-radius="15" width="70%" height="120px" v-model="show" mode="center">
			<view style="padding: 20px 15px 20px 65px;">
				确定删除该商品吗?
			</view>
			<view style="display: flex;justify-content: center;align-items: center;height: 50px;">
				<u-button @click="cancel" style="margin-right: 15px;" type="info">取消</u-button>
				<u-button @click="confirm" style="margin-left: 15px;" :custom-style="customStyle" type="error">确定
				</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script setup>
	import {
		onReady,
		onReachBottom
	} from '@dcloudio/uni-app';

	import {
		ref,
		reactive
	} from 'vue';

	// 引入api
	import {
		getMyGoodsListApi,
		upanddownpi,
		deleteApi
	} from '../../api/goods.js'

	// 删除所有静态商品数据,只留下定义
	const list = ref([])
	//列表查询的参数
	const parm = reactive({
		userId: uni.getStorageSync("userId"),
		type: '0', //闲置商品
		currentPage: 1,
		pageSize: 5 //每页5条数据
	})

	//总页数
	const pages = ref(0)
	//获取列表数据
	const getMyUnusedList = async () => {
		let res = await getMyGoodsListApi(parm)
		if (res && res.code == 200) {
			console.log(res)
			pages.value = res.data.pages; //总页数
			list.value = list.value.concat(res.data.records) //商品数据
		}
	}

	// 加载状态
	const loadstatus = ref('loadmore')
	// 触底加载
	onReachBottom(() => {

		if (parm.currentPage >= pages.value) {
			loadstatus.value = "nomore"
			return;
		}
		loadstatus.value = "loading"
		parm.currentPage = ++parm.currentPage;
		getMyUnusedList()
	})

	// 跳转到编辑商品页面
	const editBtn = (item) => {
		uni.navigateTo({
			url: "../unused_edit/unused_edit?goods=" + JSON.stringify(item)
		})
	}

	// 跳转到查看闲置页面
	const lookBtn = (item) => {
		uni.navigateTo({
			url: "../look_my_unused/look_my_unused?goods=" + JSON.stringify(item)
		})
	}

	//上下架
	const upanddownBtn = async (item) => {
		console.log(item)
		let res = await upanddownpi({
			goodsId: item.goodsId,
			status: item.status == '1' ? '0' : '1'
		})
		if (res && res.code == 200) {
			list.value = []
			parm.currentPage = 1;
			getMyUnusedList()
		}
	}

	//删除
	const deleteId = ref('')
	const show = ref(false)
	//删除按钮点击事件
	const deleteBtn = (item) => {
		// console.log(item)
		//获取要删除的商品id
		deleteId.value = item.goodsId;
		show.value = true; //显示删除弹窗
		console.log(deleteId.value)
	}
	// 取消删除
	const cancel = () => {
		show.value = false;
	}
	// 确认删除
	const confirm = async () => {
		let res = await deleteApi({
			goodsId: deleteId.value
		})
		if (res && res.code == 200) {
			list.value = []
			parm.currentPage = 1;
			getMyUnusedList()//重新获取商品列表
			show.value = false;
		}
	}

	onReady(() => {
		getMyUnusedList()
	})
</script>

<style lang="scss">
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
				margin-right: 10px;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}
</style>
